1609A - Divide and Multiply - CodeForces Solution


greedy implementation math number theory *900

Please click on ads to support us..

Python Code:

import sys
input=lambda:sys.stdin.readline().rstrip()
def solve():
	N=int(input())
	a=list(map(int,input().split()))
	p=0
	for i in range(N):
		while a[i]%2==0:
			a[i]//=2
			p+=1
	a=sorted(a)
	print(sum(a[:N-1])+a[N-1]*2**p)
T=int(input())
for i in range(T):
	solve()

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fast ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define answer cout << ans1 << "\n"
using namespace std;
ll sum=0,d,ans2=1,maxi=0,mini=0,k;
ll tt=1,n,m,a,b,c,w,mod=7+1e9,ans1=0,h;
//bool f=0;
string s,t;
vector<ll> v,v2;


bool isKthBitSet(int n, int k)
{
  return ((n & (1 << k)) ? 1 : 0);
}

ll power(ll aa,ll bb, ll momo)
{
  ll res=1;
  while(bb)
  {
    if(bb%2)res=(res*aa)%momo;
    aa=(aa*aa)%momo;
    bb/=2;
  }
  return res;
}

ll modInverse(ll x,int p)
{
    return power(x, p - 2, p);
}

ll nCrModPFermat(ll x,int r, int p)
{
    // If n<r, then nCr should return 0
    if (x < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;

    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long fac[x + 1];
    fac[0] = 1;
    for (int i = 1; i <= x; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[x] * modInverse(fac[r], p) % p * modInverse(fac[x - r], p) % p) % p;
}

string tostr(ll x)
{
  string s;
  while(x>9)
  {
    int tem=x%10;
    s.insert(s.begin(),char(tem+48));
    x/=10;
  }
  if(x!=0)s.insert(s.begin(),char(x+48));
  if(s.empty())s.insert(s.begin(),'0');
  return s;
}

bool ispalin(string s)
{
  int l=0,r=s.size()-1;
  bool ret=1;
  while(l<r)
  {
    if(s[l]!=s[r]){ret=0;break;}
    l++;r--;
  }
  return ret;
}

long long gcd(long long int a, long long int b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}

void solve()
{
  cin >> n;a=0;b=0;c=0;ans1=0;
  multiset<ll> st;
  for(int i=0;i<n;i++){cin >> a;while(a%2==0){a/=2;c++;}st.insert(a);b=max(b,a);}
  st.erase(st.find(b));
  ans1+=b*power(2,c,1e18);
  for(auto x : st)ans1+=x;
  answer;
}


int main(){
  fast;
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  cin >> tt;
  while(tt--)
  {
    solve();
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations